<?php
namespace Admin\Controller;
use Think\Controller;
class AdmincenterController extends CommonController {


	public function _initialize(){
		parent::_initialize();
	}
	
	//显示个人中心页面
	public function index(){
	       	$this->display("index");
	}
	//修改个人基本信息
	public function saveInf(){
		$admin_id =$_SESSION['ADMINID'] ;
		$Admin = M('Admin'); 
		$checkrules = array(
 			array('name','require','用户名必须填写！'), // 昵称必须
 			array('name','','帐号名称已经存在！',1,'unique',1), // 验证用户名是否已经存在
			//array('content','require','内容必须填写！'), // 内容必须
		);
		$data=$Admin->validate($checkrules)->create();
		if (!$data) {
			$this->error($Admin->getError());
		}else{
			$data['signature'] = I('post.signature','','');
			$headimg=I('post.imgs','','');
			$data['headimg']  =  json_encode($headimg);
			$res=$Admin->where("id ={$admin_id}")->save($data);
			if (!$res) {
				$this->error("请确认信息是否填写正确！");
			}else{
				$_SESSION['ADMINNAME'] = I('post.name','','');
				$_SESSION['ADMINIMG'] = $headimg[0];
				$_SESSION['ADMINSIGNATURE'] =I('post.signature','','');
				$this->success('修改个人信息成功！');
			}
		}
		
	}

	//修改账号密码
	public function savePassword(){
		$admin_id =$_SESSION['ADMINID'];
		$Admin = M('Admin'); 
		$oldpwd = I('post.oldpwd');
		$res=$Admin->where("id={$admin_id} AND password='{$oldpwd}' ")->find();
		if (!$res) {
			$this->error("旧密码输入错误！");
		}else{
			$newpwd = I('post.newpwd');
			$renewpwd = I('post.renewpwd');
			if (strlen($newpwd)<8 || strlen($newpwd)>16) {
			 	$this->error("密码长度为8-16位！");
			}else{
				
				if($newpwd !=$renewpwd) {
				 	$this->error("重复密码与新密码不一致！");
				}else{
					$data['password'] =$newpwd;
					$res=$Admin->where("id={$admin_id}")->save($data);
					if (!$res) {
						$this->error("修改账号密码失败");
					}else{
						$this->success("修改账号密码成功");
					}
				}
			}
		}
	}
}